﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Service;

namespace ECommerce.Administration.Controls
{
	public partial class AddProductCategory : AdminBaseUsercontrol
	{
		private ICategoryService _CategoryService;

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}

			//if (!IsPostBack)
			//{
			if (CommonHelper.QueryStringInt("categoryid") > 0)
			{
				LabelEditCategoryTitle.Text = GetLocalResourceString("Admin.EditProductCategory.Edit.Title");
			}
			else
			{
				LabelEditCategoryTitle.Text = GetLocalResourceString("Admin.EditProductCategory.Add.Title");
			}

			ButtonSave.Text = GetLocalResourceString("Admin.SaveButtonText");

			BindTopCategory();

			SetValues();
			//}
		}

		private void SetValues()
		{
			var categoryid = CommonHelper.QueryStringInt("categoryid");
			if (categoryid > 0)
			{
				if (_CategoryService == null)
				{
					_CategoryService = new CategoryService();
				}

				var theCategory = _CategoryService.GetCategoryByID(categoryid);

				TextCategoryName.Text = theCategory.CategoryName;
				TextDescription.Text = theCategory.Description;
				TextDisplayOrder.Text = theCategory.DisplayOrder.ToString();
				DropDownListTopCategory.SelectedValue = theCategory.ParentCategoryID.ToString();
				CheckPublished.Checked = theCategory.Published;
			}
		}

		private void BindTopCategory()
		{
			var lang = SiteContext.Current.WorkingLanguage;

			if (lang != null)
			{
				if (_CategoryService == null)
				{
					_CategoryService = new Service.CategoryService();
				}

				var categoryByLang = _CategoryService.GetCategoryByLanguage(SiteContext.Current.WorkingLanguage.LanguageId);

				if (categoryByLang != null)
				{
					var topCategories = categoryByLang.Where(e => e.ParentCategoryID <= 0);

					DropDownListTopCategory.DataSource = topCategories;
					DropDownListTopCategory.DataBind();

					DropDownListTopCategory.Items.Insert(0, new ListItem("作为父级类别", "0"));
				}
				else
				{
					DropDownListTopCategory.DataSource = null;
					DropDownListTopCategory.DataBind();

					DropDownListTopCategory.Items.Insert(0, new ListItem("作为父级类别", "0"));
				}
			}
		}

		protected void ButtonSave_Click(object sender, EventArgs e)
		{
			if (SiteContext.Current.WorkingLanguage != null)
			{
				Data.Entity.Category theCategory = new Data.Entity.Category();
				theCategory.CategoryName = TextCategoryName.Text.Trim();
				theCategory.Deleted = false;
				theCategory.Description = TextDescription.Text.Trim();
				theCategory.DisplayOrder = int.Parse(TextDisplayOrder.Text.Trim());
				theCategory.LanguageID = SiteContext.Current.WorkingLanguage.LanguageId;
				theCategory.ParentCategoryID = int.Parse(DropDownListTopCategory.SelectedValue);
				theCategory.Published = CheckPublished.Checked;
				theCategory.UpdatedOn = DateTime.Now;

				if (_CategoryService == null)
				{
					_CategoryService = new Service.CategoryService();
				}

				var categoryID = CommonHelper.QueryStringInt("categoryid");

				if (categoryID > 0)
				{
					theCategory.CategoryID = categoryID;
					_CategoryService.UpdateCategory(theCategory);
				}
				else
				{
					theCategory.CreatedOn = DateTime.Now;

					_CategoryService.CreateNewCategory(theCategory);
				}
			}
		}
	}
}